存储过程中动态条件的添加 where条件 in条件中字符串参数 您所在的位置:网站首页 oracle 调用存储过程语句 存储过程中动态条件的添加 where条件 in条件中字符串参数

存储过程中动态条件的添加 where条件 in条件中字符串参数

2024-07-11 02:48| 来源: 网络整理| 查看: 265

 

    这样写 ,数据库分析成为了

         Select 姓名,性别,年龄 From  Persons where Name =@ ParamIDList

(和上面的语句是等价的)

    这样情况的原因是因为数据库不会去分析变量中的, 号,并将变量拆分开来重新组合成新的SQL语句。

 

目录:

1.0       Table 变量和临时表,数据库中的表的区别和相同点

2.0       解决上面问题的两种途径

2.1 使用 Exec(SQL命令字符串)的方式 动态的返回数据

2.2 使用Table数据类型方式解决

  

1.0   Table 变量和临时表,数据库中的表的区别和相同点

       Table变量的概念

         Table变量是一种特殊的数据类型,在SQL程序中可以将多维的信息零时的保存到Table变量中,供后  续处理,该数据类型保存数据的形式为一组行,这些行将可作为表值函数的结果集返回。

Table 变量不能作为 存储过程,和自定义函数的变量

 

   临时表

   由会话创建于tempdb 数据库的 sysobjects 表中的临时表 ,

 

 Table和临时表,数据库中表的区别:

对SQL语句的支持

Table 变量

对于表的列的约束类型仅仅为PRIMARY KEY、UNIQUE KEY 和 NULL

INSERT INTO table_variable EXEC 存储过程。

支持基本的SELECT、INSERT、UPDATE 和 DELETE基本的表操作语句

 

临时表和数据库中实际的表 对表的语句的支持是一致(全部支持)。

 

实际的存储位置和生存的时间

Table 对象 和其他的类型的变量生成的周期是一样的,有明确的作用域,在超过作用域时系统自动的删除Table对象。

Table 对象实际上和其他类型的变量一样保存在内存中

 

临时表和数据库中的表都存储在实际的数据库中

临时表 #TableName的作用域是当前的会话中

              ##TableName 的作用域为全局量

 

 

2.0解决上面问题的两种途径

          2.1 使用 Exec(SQL命令字符串)的方式 动态的返回数据

sql 字符串参数 - qingtiansunsky - The world in my eyesDeclare @Command  Varchar(1000 )   sql 字符串参数 - qingtiansunsky - The world in my eyesDeclare  @ParamIDList Varchar(500 )sql 字符串参数 - qingtiansunsky - The world in my eyesSet @ ParamIDList= ’张三,李四,王五’sql 字符串参数 - qingtiansunsky - The world in my eyesSet   @Command  =’Select 姓名,性别,年龄 From  Persons where Name In(’+@ ParamIDList+ ’)’sql 字符串参数 - qingtiansunsky - The world in my eyesExec(@Command )sql 字符串参数 - qingtiansunsky - The world in my eyes

         2.2 使用Table数据类型方式解决

 

sql 字符串参数 - qingtiansunsky - The world in my eyes sql 字符串参数 - qingtiansunsky - The world in my eyes /**/ /* 根据输入的@ParamIDList列表来生Name列表*/ sql 字符串参数 - qingtiansunsky - The world in my eyes  Declare    @ParamIDList   Varchar ( 500 )sql 字符串参数 - qingtiansunsky - The world in my eyes D eclare   @Table_NameList   table  ( Name  Varchar ( 20 ))   --  建立表变量     Declare   @Index_Param   int     /**/ /*参数 记录分隔符的位置*/    Declare   @NeedParse   varchar ( 500 )  /**/ /*参数 没有处理的字符串*/ sql 字符串参数 - qingtiansunsky - The world in my eyessql 字符串参数 - qingtiansunsky - The world in my eyes  Select    @Index_Param = CharIndex ( ' , ' ,  @ParamIDList )sql 字符串参数 - qingtiansunsky - The world in my eyes  if  ( @Index_Param = 0 )sql 字符串参数 - qingtiansunsky - The world in my eyessql 字符串参数 - qingtiansunsky - The world in my eyes begin          /**/ /*一个名字组成*/ sql 字符串参数 - qingtiansunsky - The world in my eyes           insert   into   @Table_NameList  (Name)  values ( @ParamIDList )sql 字符串参数 - qingtiansunsky - The world in my eyes  end sql 字符串参数 - qingtiansunsky - The world in my eyessql 字符串参数 - qingtiansunsky - The world in my eyes else       /**/ /*存在多个名字*/ sql 字符串参数 - qingtiansunsky - The world in my eyes         begin sql 字符串参数 - qingtiansunsky - The world in my eyes              set   @NeedParse   = @ParamIDList sql 字符串参数 - qingtiansunsky - The world in my eyes              while  ( CharIndex ( ' , ' ,  @NeedParse ) > 0 )sql 字符串参数 - qingtiansunsky - The world in my eyes                  begin sql 字符串参数 - qingtiansunsky - The world in my eyes                          insert   into   @Table_NameList  (Name)  values ( SubString ( @BeginString , 1 , CharIndex ( ' , ' , @BeginString ) - 1 ))sql 字符串参数 - qingtiansunsky - The world in my eyes                         set   @NeedParse   = SubString ( @NeedParse , CharIndex ( ' , ' ,  @NeedParse ) + 1 , len ( @NeedParse ) - CharIndex ( ' , ' ,  @NeedParse ))sql 字符串参数 - qingtiansunsky - The world in my eyes             end sql 字符串参数 - qingtiansunsky - The world in my eyes         insert   into   @Table_NameList  (Name)  values ( @NeedParse )sql 字符串参数 - qingtiansunsky - The world in my eyes     end sql 字符串参数 - qingtiansunsky - The world in my eyessql 字符串参数 - qingtiansunsky - The world in my eyessql 字符串参数 - qingtiansunsky - The world in my eyes Select  姓名,性别,年龄  From   Persons  where  Name  In ( select  Name  from   @Table_NameList )


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有